package com.edu.exam.entity.grading.correction;

import com.edu.exam.enums.grading.CorrectionTaskPriority;
import com.edu.exam.enums.grading.CorrectionTaskType;
import com.edu.exam.enums.grading.GradingTaskStatus;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.time.LocalDateTime;
import java.time.Duration;

/**
 * 修正任务实体类
 *
 * <p>所属模块：E-02 阅卷管理模块</p>
 * <p>对应数据库表：correction_task</p>
 *
 * <p>记录成绩修正的任务信息，包括任务类型、任务状态、
 * 任务描述、受理人、执行时间等关键信息。支持任务流程管理。</p>
 *
 * @author System
 * @version 1.0.0
 */
@Entity
@EntityListeners(AuditingEntityListener.class)
@Data
@Builder
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class CorrectionTask {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotNull(message = "修正ID不能为空")
    @Column(name = "correction_id", nullable = false)
    private Long correctionId;

    @NotNull(message = "任务类型不能为空")
    @Enumerated(EnumType.STRING)
    @Column(name = "task_type", nullable = false, length = 20)
    private CorrectionTaskType taskType;

    @NotNull(message = "任务状态不能为空")
    @Enumerated(EnumType.STRING)
    @Column(name = "task_status", nullable = false, length = 20)
    @Builder.Default
    private GradingTaskStatus taskStatus = GradingTaskStatus.PENDING;

    @NotBlank(message = "任务描述不能为空")
    @Size(max = 1000, message = "任务描述长度不能超过1000个字符")
    @Column(name = "task_description", nullable = false, columnDefinition = "TEXT")
    private String taskDescription;

    @NotNull(message = "受理人ID不能为空")
    @Column(name = "assignee_id", nullable = false)
    private Long assigneeId;

    @Size(max = 100, message = "受理人姓名长度不能超过100个字符")
    @Column(name = "assignee_name", length = 100)
    private String assigneeName;

    @NotNull(message = "分配时间不能为空")
    @CreatedDate
    @Column(name = "assign_time", nullable = false, updatable = false)
    private LocalDateTime assignTime;

    @Column(name = "start_time")
    private LocalDateTime startTime;

    @Column(name = "end_time")
    private LocalDateTime endTime;

    @Size(max = 1000, message = "任务结果长度不能超过1000个字符")
    @Column(name = "task_result", columnDefinition = "TEXT")
    private String taskResult;

    @Size(max = 1000, message = "任务备注长度不能超过1000个字符")
    @Column(name = "task_remarks", columnDefinition = "TEXT")
    private String taskRemarks;

    @Enumerated(EnumType.STRING)
    @Column(name = "task_priority", length = 20)
    @Builder.Default
    private CorrectionTaskPriority taskPriority = CorrectionTaskPriority.NORMAL;

    @Column(name = "estimated_duration_minutes")
    private Integer estimatedDurationMinutes;

    @Column(name = "actual_duration_minutes")
    private Integer actualDurationMinutes;

    @Column(name = "deleted", nullable = false)
    @Builder.Default
    private Boolean deleted = false;

    @LastModifiedDate
    @Column(name = "update_time", nullable = false)
    private LocalDateTime updateTime;

    @Column(name = "create_user", nullable = false)
    private Long createUser;

    @Column(name = "update_user", nullable = false)
    private Long updateUser;

    // ===== 标准审计字段 =====

    /**
     * 创建时间（标准审计字段）
     */
    @CreationTimestamp
    @Column(name = "created_time_audit", nullable = false, updatable = false)
    private LocalDateTime createdTimeAudit;

    /**
     * 更新时间（标准审计字段）
     */
    @UpdateTimestamp
    @Column(name = "updated_time_audit", nullable = false)
    private LocalDateTime updatedTimeAudit;

    /**
     * 版本号（乐观锁）
     */
    @Version
    @Column(name = "version_audit")
    Long versionAudit = 0L;

    /**
     * 逻辑删除标记（标准审计字段）
     */
    @Column(name = "is_deleted_audit", nullable = false)
    Boolean isDeletedAudit = false;
}